package com.ly.hotel.view.floatWindows.monitor;

import java.lang.ref.WeakReference;

import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.PixelFormat;
import android.net.Uri;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.view.View.OnTouchListener;
import android.view.WindowManager.LayoutParams;
import android.widget.MediaController;
import android.widget.RelativeLayout;
import android.widget.VideoView;

import com.ly.hotel.R;
import com.ly.hotel.common.Constants;

public class FloatVideoWin extends Service {
	// 定义浮动窗口布局
//	LinearLayout mFloatLayout;
	private RelativeLayout mFloatLayout;
	LayoutParams wmParams;
	// 创建浮动窗口设置布局参数的对象
	WindowManager mWindowManager;

//	ImageButton mFloatView;
	private ThisHandler handler;
	private VideoView videoView;
	private MediaController  mediaco;  
	Context context;
	
	private float upx = 0;
	private float upy = 0;
	private float lastX = 0;
	private float lastY = 0;
	@Override
	public void onCreate() {
		super.onCreate();
		context = getBaseContext();
		
		handler = new ThisHandler(this);
		createFloatView();
		
		registerReceiver(bottomMenuRev, new IntentFilter(Constants.Broadcast.FLOAT_VIDEO));
		
//		sendBroadcast(new Intent(Constants.Broadcast.SHUAKA_TIPS).putExtra("flag", Constants.CLOSE_TIPS));
	}


	@Override
	public IBinder onBind(Intent intent) {
		return null;
	}

	private void createFloatView() {
		wmParams = new LayoutParams();
		getApplication();
		// 获取WindowManagerImpl.CompatModeWrapper
		mWindowManager = (WindowManager) getApplication().getSystemService(Context.WINDOW_SERVICE);

		DisplayMetrics dm = new DisplayMetrics();
		mWindowManager.getDefaultDisplay().getMetrics(dm);

		// 设置window type
		wmParams.type = LayoutParams.TYPE_PHONE;
		// 设置图片格式，效果为背景透明
		wmParams.format = PixelFormat.RGBA_8888;
		// 设置浮动窗口不可聚焦（实现操作除浮动窗口外的其他可见窗口的操作）
		wmParams.flags = 
		// LayoutParams.FLAG_NOT_TOUCH_MODAL |
		LayoutParams.FLAG_NOT_FOCUSABLE
		// LayoutParams.FLAG_NOT_TOUCHABLE
		;

		// 调整悬浮窗显示的停靠位置为左侧置顶
		wmParams.gravity = Gravity.CENTER;
 
		// 设置悬浮窗口长宽数据
		wmParams.width = 600;
		wmParams.height = 600; 

		LayoutInflater inflater = LayoutInflater.from(getApplication());
		// 获取浮动窗口视图所在布局 
		// 获取浮动窗口视图所在布局
		mFloatLayout = (RelativeLayout) inflater.inflate(R.layout.float_video, null);
		mWindowManager.addView(mFloatLayout, wmParams);

		videoView = (VideoView) mFloatLayout.findViewById(R.id.video);
		mediaco = new MediaController(context);  
		
//		videoView.setMediaController(mediaco);  
//        mediaco.setMediaPlayer(videoView);  
//        mediaco.setOnTouchListener(null);
//		videoView.setVisibility(View.VISIBLE);
		
		mFloatLayout.setOnTouchListener(new OnTouchListener() {
			@Override
			public boolean onTouch(View v, MotionEvent event) {

				switch (event.getAction()) {
				case MotionEvent.ACTION_DOWN:
					// 记录点击时距离起始点的位置
					upx = event.getRawX() - wmParams.x;
					upy = event.getRawY() - wmParams.y;
					break;
				case MotionEvent.ACTION_MOVE:
					wmParams.x = (int) (event.getRawX() - upx);
					wmParams.y = (int) (event.getRawY() - upy);
//					if (wmParams.y <= 70) {
//						wmParams.y = 70;
//					}
					mWindowManager.updateViewLayout(mFloatLayout, wmParams);
					break;
				case MotionEvent.ACTION_UP:

					if (Math.abs(lastX - wmParams.x) > 10 || Math.abs(lastY - wmParams.y) > 10) {
						lastX = wmParams.x;
						lastY = wmParams.y;
						break;
					}

					lastX = wmParams.x;
					lastY = wmParams.y;

					v.performClick();

					break;
				}
				return false;
			}
		});
		videoView.setVideoURI(Uri.parse("rtsp://admin:pre222222@172.16.1.64:554/h264/ch1/main/av_stream"));
		videoView.start();
	}

	@Override
	public int onStartCommand(Intent intent, int flags, int startId) {
		return super.onStartCommand(intent, flags, startId);
	}

	@Override
	public void onDestroy() {
		super.onDestroy();
	}


	/* 这个就是是否显示浮动小圆点的事件接口 */
	BroadcastReceiver bottomMenuRev = new BroadcastReceiver() {
		@Override
		public void onReceive(Context cont, Intent inte) {
			int flag = inte.getIntExtra("flag", 0);
			if (flag == Constants.SHOW_FLOAT_VIDEO) {// 显示
				handler.post(new Runnable() {

					@Override
					public void run() {
						videoView.setVisibility(View.VISIBLE);
					}
				});
			} else if (flag == Constants.CLOSE_TIPS) {
				handler.post(new Runnable() { 

					@Override
					public void run() {
						videoView.setVisibility(View.INVISIBLE);
					}
				});
			}
		}
	};
	
	private static class ThisHandler extends Handler {
		private WeakReference<FloatVideoWin> wrActivity;

		public ThisHandler(FloatVideoWin activity) {
			wrActivity = new WeakReference<FloatVideoWin>(activity);
		}

		@Override
		public void handleMessage(Message message) {
			FloatVideoWin act = wrActivity.get();
			if (act != null) {
				// 这个是通过事实消息接口获取的弹窗信息打开的
				if (message.what == 0) {
					act.sendBroadcast(new Intent(Constants.Broadcast.FLOAT_VIDEO).putExtra("flag", Constants.CLOSE_FLOAT_VIDEO));
				}
			}
		}
	}

}
